<?php

namespace app\api\controller;

use think\Controller;
use think\Db;

/**
 *  支付
 */
class Wechat extends Basic
{

    /**
     * 获取用户基本信息
     */
    public function getUserDetail($userId)
    {
        if ($userId == null) {
            return false;
        }

        // 查询用户数据
        $User = Db('User');
        // 获取用户);
        $userInfo = $User->where(array('id' => $userId))->find();
        if (!$userInfo) {
            return false;
        }
        return true;
    }

    /**
     * 生成微信支付订单请求一分钱
     * jihaichuan
     */
    public function pay_red_cent()
    {
        //用户id
        $user_id = input('user_id');
        $userId = decode($user_id);
        $openid = Db('User')->where(array('id' => $userId))->value('openid');
        // 回调地址
        $notify_url = config('SITE_URL') . '/Wechat/order_notify_url';
        // 调用
        require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/Wxpay/WxPay.Api.php');
        require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/Wxpay/WxPayUnifiedOrder.php');
        require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/Wxpay/WxPayResults.php');
        require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/vendor/Wxpay/JsApiPay.php');
        $number = orderNum();

        $input = new \WxPayUnifiedOrder();
        $input->SetBody('环球人物一分钱');
        $input->SetAttach('小程序订单：' .$number);
        $input->SetOut_trade_no($number);
        $input->SetTotal_fee(1);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("订单支付");
        $input->SetNotify_url($notify_url);
        $input->SetTrade_type('JSAPI');
        $input->SetOpenid($openid);

        $order = \WxPayApi::unifiedOrder($input);

        $tools = new \JsApiPay();
        // 创建调用信息
        $jsApiParameters = $tools->GetJsApiParameters($order);
        
        echo $jsApiParameters;
    }

}

?>